package com.kycxika.app.activity.twoactivity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.kycxika.app.R;
import com.kycxika.app.activity.LoginActivity;
import com.kycxika.app.activity.MainActivity;
import com.kycxika.app.activity.base.BaseActivity;
import com.kycxika.app.activity.pic.SelfInfoActivity;
import com.kycxika.app.dbutils.IntImp.KycXkIApi;
import com.kycxika.app.utils.SharedPreferencesHelp;
import com.kycxika.app.view.CircleImageView;
import com.zhy.android.percent.support.PercentRelativeLayout;

import static com.kycxika.app.KycXikaApplication.getContext;

public class MeActivity extends BaseActivity {

    private CircleImageView mFamily_image;
    private TextView family_self;
    private TextView family_name;
    private PercentRelativeLayout out_login;
    private Boolean userRegistionStatu;
    private KycXkIApi api;
    private View mTop;

    @Override
    protected void initView() {
        mTop = findViewById(R.id.top);
        setTop(mTop);
        getVisibility(true, MeActivity.this);
        getTitle("我的");

        mFamily_image = (CircleImageView) findViewById(R.id.family_image);
        family_self = (TextView) findViewById(R.id.family_self);   //个人信息
        family_name = (TextView) findViewById(R.id.family_name);
        out_login = (PercentRelativeLayout) findViewById(R.id.out_login);

        api = KycXkIApi.getInstance(getContext());
        userRegistionStatu = SharedPreferencesHelp.getInstance().getLoginStatus();//api.getUserRegistionStatu();  //判断是否登录

        if (userRegistionStatu) {
            out_login.setVisibility(View.VISIBLE);
            out_login.setEnabled(true);
            family_name.setText(SharedPreferencesHelp.getInstance().getUserNick());
            if (!TextUtils.isEmpty(SharedPreferencesHelp.getInstance().getUserHeader())) {
                Log.i("TAG", "header ------ " + SharedPreferencesHelp.getInstance().getUserHeader());
                /*Bitmap bitmap = BitmapFactory.decodeFile(SharedPreferencesHelp.getInstance().getUserHeader());
                mFamily_image.setImageBitmap(bitmap);*/
                Glide.with(this)
                        .load(SharedPreferencesHelp.getInstance().getUserHeader())
                        .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存多个尺寸
                        .thumbnail(0.1f)//先显示缩略图  缩略图为原图的1/10
                        .error(R.drawable.ce_01)
                        .into(new GlideDrawableImageViewTarget(mFamily_image) {
                            @Override
                            public void onLoadStarted(Drawable placeholder) {
                                super.onLoadStarted(placeholder);
                            }

                            @Override
                            public void onLoadFailed(Exception e, Drawable errorDrawable) {
                                super.onLoadFailed(e, errorDrawable);
                            }

                            @Override
                            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
                                super.onResourceReady(resource, animation);
                            }
                        });
            } else {
                mFamily_image.setImageResource(R.drawable.ce_01);
            }
        } else {
            out_login.setVisibility(View.INVISIBLE);
            out_login.setEnabled(false);
            family_name.setText("请登录");
            mFamily_image.setImageResource(R.drawable.ce_01);
        }
    }

    public void initData() {

    }

    public void initListener() {
        mFamily_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!userRegistionStatu) {
                    startActivity(new Intent(MeActivity.this, LoginActivity.class));
                    MeActivity.this.finish();
                } else {

                }
            }
        });

        findViewById(R.id.system_notif).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //系统通知
                if (userRegistionStatu) {

                } else {
                    startActivity(new Intent(MeActivity.this, LoginActivity.class));
                }
            }
        });
        findViewById(R.id.system_setting).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //系统设置
                if (userRegistionStatu) {

                } else {
                    startActivity(new Intent(MeActivity.this, LoginActivity.class));
                }
            }
        });
        findViewById(R.id.out_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //退出登录
                SharedPreferencesHelp.getInstance().saveLoginStatus(false);
                startActivity(new Intent(MeActivity.this, LoginActivity.class));
                MainActivity.instance.finish();
            }
        });

        family_self.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //个人设置
                if (userRegistionStatu) {
                    startActivity(new Intent(MeActivity.this, SelfInfoActivity.class));
                } else {
                    startActivity(new Intent(MeActivity.this, LoginActivity.class));
                }
                MeActivity.this.finish();
                //MainActivity.instance.finish();
            }
        });
    }

    @Override
    public int getLayout() {
        return R.layout.activity_me;
    }
}
